package com.chan.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.chan.common.core.domain.Ztree;
import com.chan.common.core.text.Convert;
import com.chan.common.utils.DateUtils;
import com.chan.service.ICdProgramService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chan.mapper.CdProgramMapper;
import com.chan.domain.CdProgram;


/**
 * 项目集Service业务层处理
 *
 * @author chan
 * @date 2024-04-08
 */
@Service
public class CdProgramServiceImpl implements ICdProgramService
{
    @Autowired
    private CdProgramMapper cdProgramMapper;

    /**
     * 查询项目集
     *
     * @param id 项目集主键
     * @return 项目集
     */
    @Override
    public CdProgram selectCdProgramById(Integer id)
    {
        return cdProgramMapper.selectCdProgramById(id);
    }

    /**
     * 查询项目集列表
     *
     * @param cdProgram 项目集
     * @return 项目集
     */
    @Override
    public List<CdProgram> selectCdProgramList(CdProgram cdProgram)
    {
        return cdProgramMapper.selectCdProgramList(cdProgram);
    }

    /**
     * 新增项目集
     *
     * @param cdProgram 项目集
     * @return 结果
     */
    @Override
    public int insertCdProgram(CdProgram cdProgram)
    {
        cdProgram.setCreateTime(DateUtils.getNowDate());
        return cdProgramMapper.insertCdProgram(cdProgram);
    }

    /**
     * 修改项目集
     *
     * @param cdProgram 项目集
     * @return 结果
     */
    @Override
    public int updateCdProgram(CdProgram cdProgram)
    {
        cdProgram.setUpdateTime(DateUtils.getNowDate());
        return cdProgramMapper.updateCdProgram(cdProgram);
    }

    /**
     * 批量删除项目集
     *
     * @param ids 需要删除的项目集主键
     * @return 结果
     */
    @Override
    public int deleteCdProgramByIds(String ids)
    {
        return cdProgramMapper.deleteCdProgramByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目集信息
     *
     * @param id 项目集主键
     * @return 结果
     */
    @Override
    public int deleteCdProgramById(Integer id)
    {
        return cdProgramMapper.deleteCdProgramById(id);
    }

    /**
     * 查询项目集树列表
     *
     * @return 所有项目集信息
     */
    @Override
    public List<Ztree> selectCdProgramTree()
    {
        List<CdProgram> cdProgramList = cdProgramMapper.selectCdProgramList(new CdProgram());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (CdProgram cdProgram : cdProgramList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(cdProgram.getId().longValue());
            ztree.setpId(cdProgram.getParentId().longValue());
            ztree.setName(cdProgram.getName());
            ztree.setTitle(cdProgram.getName());
            ztree.setType(1);
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
